<?php

namespace App\Admin\Controllers;

use App\Models\Category;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Tree;
use Encore\Admin\Widgets\Box;
use Illuminate\Routing\Controller;

class CategoryController extends Controller
{
    use ModelForm;
    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('分类标签列表');
            $content->description('分类标签列表');

            $content->row(function (Row $row) {
                $row->column(6, $this->treeView()->render());

                $row->column(6, function (Column $column) {
                    $form = new \Encore\Admin\Widgets\Form();
                    $form->action(admin_base_path('category'));

                    $form->select('parent_id', '父级')->options(Category::selectOptions());
//                    $form->select('type', '类型')->options([
//                        'topic' => 'topic',
//                        'article' => 'article'
//                    ])->rules('required');

                    $form->text('title', '名称')->rules('required');
                    $form->text('nickname', '昵称')->rules('required');
                    $form->select('flag', '推荐');
//                        ->options(['index', 'hot']);
                    $form->image('image', '图标');
                    $form->textarea('description', '介绍');
                    $form->number('order', '排序')->default(0);
                    $form->radio('status', '状态')->options([
                        '禁用', '显示'
                    ])->default(1);

                    $column->append((new Box('添加', $form))->style('success'));
                });
            });
        });
    }

    /**
     * Redirect to edit page.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function show($id)
    {
        return redirect()->route('category.edit', ['id' => $id]);
    }

    /**
     * @return \Encore\Admin\Tree
     */
    protected function treeView()
    {
        $list = Category::tree(function (Tree $tree) {
            $tree->disableCreate();

            $tree->branch(function ($branch) {

                $payload = "<strong>{$branch['title']}</strong>";

                if (!isset($branch['children'])) {
                    $status = '';
                    if ($branch['status'] == 0){
                        $status = "<span style='color: red'><i class='fa fa-times-circle'></i> 禁用</span>";
                    }
                    $payload .= "&nbsp;&nbsp;&nbsp;<span style='font-size: 0.85em; color: #585858'>{$branch['description']} &nbsp;{$status}</span>";
                }

                return $payload;
            });
        });

       return $list;
    }

    /**
     * Edit interface.
     *
     * @param string $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header(trans('admin.menu'));
            $content->description(trans('admin.edit'));

            $content->row($this->form()->edit($id));
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    public function form()
    {
        return Category::form(function (Form $form) {
            $form->display('id', 'ID');

            $form->select('parent_id', '父级')->options(Category::selectOptions());
//            $form->select('type', '类型')->options(['topic', 'article'])->rules('required');
            $form->text('title', '名称')->rules('required');
            $form->text('nickname', '昵称')->rules('required');
            $form->select('flag', '推荐');
//                        ->options(['index', 'hot']);
            $form->image('image', '图标');
            $form->textarea('description', '介绍');
            $form->number('order', '排序')->default(0);
            $form->radio('status', '状态')->options([
                '禁用', '显示'
            ])->default(1);

            $form->saved(function (){
                $categoryService = app('App\Services\CategoryService');
                $categoryService->setCategoriesCache();
            });

            $form->display('created_at', trans('admin.created_at'));
            $form->display('updated_at', trans('admin.updated_at'));
        });
    }

}
